package programer.interviewer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class Question7 {
    //[[1,2,3],            //[[7,4,1],
    // [4,5,6],   ==>      // [8,5,2],
    // [7,8,9]]            // [9,6,3]]

    public void rotate(int[][] matrix) {
        int row = matrix.length;


        //[0,0]=>[0,2]    [1,0]=>[0,1]  [2,0] => [0,0]
        //[0,1]=>[1,2]    [1,1]=>[1,1]  [2,1] => [1,0]
        //[0,2]=>[2,2]    [1,2]=>[2,1]  [2,2] => [2,0]
        System.out.println("row:" + row);
        Arrays.asList(matrix).forEach((it -> System.out.println(Arrays.toString(it))));

        int temp;
        for(int i = 0; i < row; i++) {
            for(int j=i;j<row;j++) {
                if(i == j) {
                    continue;
                }
                temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = temp;
            }
        }
        System.out.println("after:");
        Arrays.asList(matrix).forEach((it -> System.out.println(Arrays.toString(it))));

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < row / 2 ; j++) {
                temp = matrix[i][j];
                matrix[i][j] = matrix[i][row - 1 - j];
                matrix[i][row - 1 - j] = temp;
            }
        }

        System.out.println("result:");
        Arrays.asList(matrix).forEach((it -> System.out.println(Arrays.toString(it))));
    }

    public static void main(String[] args) {
        new Question7().rotate(new int[][]{{1,2,3,5}, {5,6,7,8} , {8,9,10,11}, {12,13,14,15}});
    }
}
